﻿using Innoapps.FileTransfer.Helpers;
using Innoapps.FileTransfer.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Innoapps.FileTransfer.Providers.Input
{
    public class FileSystem : Base
    {
        #region Constructors
        public FileSystem()
        {
            watcher.Changed += Watcher_Changed;
        }

        private void Watcher_Changed(object sender, FileSystemEventArgs e)
        {
            if (e.ChangeType == WatcherChangeTypes.Created || e.ChangeType == WatcherChangeTypes.Renamed)
            {
                XmlSerializerHelper<Models.Transfer> xmlHelperData = new XmlSerializerHelper<Models.Transfer>();
                Transfer transfer = xmlHelperData.Read(e.FullPath, "http://innoapps.co.uk/FileTransfer/Transfer");
                OnInputEvent(transfer);
            }
        }
        #endregion

        #region Variables
        FileSystemWatcher watcher = new FileSystemWatcher();
        #endregion

        #region Properties
        /// <summary>
        /// Defines the folder path to be watched
        /// </summary>
        public string FolderPath { get { return watcher.Path; } set { Directory.CreateDirectory(value); watcher.Path = value; } }

        public string Filter {  get { return watcher.Filter; } set { watcher.Filter = value; } }
        #endregion

        #region Overridden methods
        public override void Start()
        {
            watcher.EnableRaisingEvents = true;
        }
        public override void Stop()
        {
            watcher.EnableRaisingEvents = false;
        }
        #endregion

    }
}
